package com.esun.common.utils;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;
import java.util.Map;

/**
 * @author ：Ocean
 * @date ：Created in 2022/1/13 16:03
 * @description：中文路径压缩相关工具类
 * @modified By：
 */
public class ZipUtils {

    /**文件读取缓冲区大小*/
    private static final int CACHE_SIZE = 1024;

    /**
     *
     * @param files
     * @param zipFilePath
     */
    public static void zipFiles(Map<File, String> files, String zipFilePath) {
        File zipFile = new File(zipFilePath);
        if(FileUtils.checkFileExists(zipFile)){
            zipFile.delete();
        }
        if(!FileUtils.checkFileExists(zipFile.getParentFile())){
            zipFile.getParentFile().mkdirs();
        }
        OutputStream os = null;
        BufferedOutputStream bos = null;
        ZipOutputStream zos = null;
        try {
            os = new FileOutputStream(zipFilePath);
            bos = new BufferedOutputStream(os);
            zos = new ZipOutputStream(bos);
            // 解决中文文件名乱码
            if (System.getProperty("os.name").contains("Windows")) {
                zos.setEncoding("GBK");
            }
            zipFiles(files, zos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if (zos != null) {
                    zos.closeEntry();
                    zos.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 打包多个文件(不考虑文件夹)
     * @param files
     * @param zos
     * @throws Exception
     */
    private static void zipFiles(Map<File, String> files, ZipOutputStream zos) throws Exception {
        String fileName;
        InputStream is;
        BufferedInputStream bis;
        byte[] cache = new byte[CACHE_SIZE];
        for(Map.Entry<File, String> entry : files.entrySet()){
            File file = entry.getKey();
            fileName = file.getName();
            is = new FileInputStream(file);
            bis = new BufferedInputStream(is);
            //替换压缩包中的文件名
            zos.putNextEntry(new ZipEntry(new File(fileName), entry.getValue()));
            int nRead = 0;
            while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
                zos.write(cache, 0, nRead);
            }
            bis.close();
            is.close();
        }
    }

//    public static void main(String[] args) throws Exception {
//        String source1 = "D:/test/c8ff7a87-c2e3-43cc-90ae-e50c555e90da1641978003704.docx";
//        String source2 = "D:/test/1/e43393be-9164-4f15-b436-edc0935091281641978445146.docx";
//        String source3 = "D:/test/1/f3f413ba-5c27-489e-b14e-f74dd0fd8c6b1641977936699.docx";
//        String zipFilePath = "D:/test/zip/压缩文件.zip";
//        Map<File, String> files = new HashMap<>();
//        files.put(new File(source1), "中文111.docx");
//        files.put(new File(source2), "2中文22.docx");
//        files.put(new File(source3), "中文333.docx");
//        ZipUtils.zip(files, zipFilePath);
//
////      CHZipUtils.unZip(zipFilePath, destDir);
//        System.out.println("********执行成功**********");
//    }
}
